//
//  BTGlassScrollView.m
//  BTGlassScrollViewExample
//
//  Created by Byte on 10/18/13.
//  Copyright (c) 2013 Byte. All rights reserved.
//

#import "BTGlassScrollView.h"
#import "Header.h"

@interface BTGlassScrollView ()


@end

@implementation BTGlassScrollView
{
    UIView *_constraitView;
    
    UIView *_foregroundContainerView; // for masking
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithBackgroundImage:(UIImage *)backgroundImage
{
    self = [super init];
    if (self) {
        //initialize values
        _backgroundImage = backgroundImage;
        
        //autoresize
        [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
        
        //create views
        [self createBackgroundView];
    }
    return self;
}

- (UIImageView *)detailImageView
{
    if (!_detailImageView) {
        self.detailImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,  SCREEN_WIDTH + DEFAULT_MAX_BACKGROUND_MOVEMENT_HORIZONTAL ,  SCREEN_HEIGHT / 2 )];
//        [_detailImageView setImageWithURL:[NSURL URLWithString:self.video.coverForFeed]];
        CGRect frame = _detailImageView.frame;
        _detailImageView.userInteractionEnabled = YES;
        
        _keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"bounds"];
        //持续时间
        _keyframeAnimation.duration = 15.0;
        //设置动画次数
        _keyframeAnimation.repeatCount = 99;
        //设置逐帧动画
        _keyframeAnimation.values = @[
            [NSValue valueWithCGRect:CGRectMake(0, 0, frame.size.width , frame.size.height)],
            [NSValue valueWithCGRect:CGRectMake(0, 0, frame.size.width + 20, frame.size.height + 20)],
            [NSValue valueWithCGRect:CGRectMake(0, 0, frame.size.width , frame.size.height)],
            [NSValue valueWithCGRect:CGRectMake(0, 0, frame.size.width + 20, frame.size.height + 20)],
            [NSValue valueWithCGRect:CGRectMake(0, 0, frame.size.width , frame.size.height)]];
        //延迟1秒
        _keyframeAnimation.beginTime = CACurrentMediaTime() + 2;
        //使得动画均匀进行
        _keyframeAnimation.rotationMode = kCAAnimationPaced;
        
        [_detailImageView.layer addAnimation:_keyframeAnimation forKey:nil];
        
    }
    return _detailImageView;
}

- (void)scrollHorizontalRatio:(CGFloat)ratio
{
    [_backgroundScrollView setContentOffset:CGPointMake(DEFAULT_MAX_BACKGROUND_MOVEMENT_HORIZONTAL + ratio * DEFAULT_MAX_BACKGROUND_MOVEMENT_HORIZONTAL, _backgroundScrollView.contentOffset.y)];
}

- (void)scrollVerticallyToOffset:(CGFloat)offsetY
{
    [_foregroundScrollView setContentOffset:CGPointMake(_foregroundScrollView.contentOffset.x, offsetY)];
}

#pragma mark - Setters
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    //work background
    CGRect bound = CGRectOffset(frame, -frame.origin.x, -frame.origin.y);
    
    [_backgroundScrollView setFrame:bound];
    [_backgroundScrollView setContentSize:CGSizeMake(bound.size.width, bound.size.height)];
    [_backgroundScrollView setContentOffset:CGPointMake( DEFAULT_MAX_BACKGROUND_MOVEMENT_HORIZONTAL , 0)];
    [_constraitView setFrame:CGRectMake(0, 0, frame.size.width + DEFAULT_MAX_BACKGROUND_MOVEMENT_HORIZONTAL, frame.size.height)];
    
    //foreground
    [_foregroundScrollView setFrame:bound];

    if (_delegate && [_delegate respondsToSelector:@selector(glassScrollView:didChangedToFrame:)]) {
        [_delegate glassScrollView:self didChangedToFrame:bound];
    }
}

- (void)createBackgroundView
{
    //background
    _backgroundScrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    [_backgroundScrollView setUserInteractionEnabled:NO];
    [_backgroundScrollView setContentSize:CGSizeMake(self.frame.size.width,self.frame.size.height)];
    [self addSubview:_backgroundScrollView];
    
    _constraitView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , self.frame.size.height)];
    [_backgroundScrollView addSubview:_constraitView];
    
    _backgroundImageView = [self detailImageView];
    
    _backgroundImageView.image = _backgroundImage;
    
    [_backgroundImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_backgroundImageView setContentMode:UIViewContentModeScaleAspectFill];
    
    [_constraitView addSubview:_backgroundImageView];
    
    [_constraitView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_backgroundImageView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_backgroundImageView)]];
    [_constraitView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_backgroundImageView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_backgroundImageView)]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    //translate into ratio to height
    CGFloat ratio = (scrollView.contentOffset.y + _foregroundScrollView.contentInset.top)/(_foregroundScrollView.frame.size.height - _foregroundScrollView.contentInset.top - _viewDistanceFromBottom);
    
    ratio = ratio < 0 ? 0 : ratio;
    ratio = ratio > 1 ? 1 : ratio;
    
    //set background scroll
    [_backgroundScrollView setContentOffset:CGPointMake(DEFAULT_MAX_BACKGROUND_MOVEMENT_HORIZONTAL, ratio * DEFAULT_MAX_BACKGROUND_MOVEMENT_HORIZONTAL)];
}


@end
